#ifndef NETWORKMANAGER_H
#define NETWORKMANAGER_H

#include <QObject>
#include <QJsonArray>
#include <QtPromise>
#include "securehttpclient.h"

struct NetErrorInfo {
    int code;
    QString message;
    QDateTime timestamp;

    operator QVariantMap() const {
        return {
            {"code", code},
            {"message", message},
            {"timestamp", timestamp}
        };
    }
};

class NetworkManager : public QObject
{
    Q_OBJECT
public:
    explicit NetworkManager(QObject *parent = nullptr);

public:
    QString getMacAddress();
    QtPromise::QPromise<QJsonObject> Login(
        const QString& tenant,
        const QString& username,
        const QString& password);
    QtPromise::QPromise<QJsonObject> QueryPrinterTemplate();
    QtPromise::QPromise<QJsonObject> QueryMerterialList();
    QtPromise::QPromise<QJsonObject> QueryMaterialRecordList();

    QtPromise::QPromise<QJsonObject> UploadRecords(QJsonArray list);
    QtPromise::QPromise<QJsonObject> ChangePassword(QString oldPassword, QString newPassword);

private:
    SecureHttpClient client;
    QString token;

signals:
    void requestFailed(const QString &error);
    void tokenExpired(const QString &error);
};

#endif // NETWORKMANAGER_H
